package com.uvnos4j.matilda.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.uvnos4j.matilda.commons.annotation.AppLog;
import com.uvnos4j.matilda.commons.dto.GridTable;
import com.uvnos4j.matilda.commons.dto.PageParam;
import com.uvnos4j.matilda.commons.dto.QueryParam;
import com.uvnos4j.matilda.commons.dto.Result;
import com.uvnos4j.matilda.commons.utils.RequestUtil;
import com.uvnos4j.matilda.system.domain.SysLog;
import com.uvnos4j.matilda.system.service.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 * 日志表 前端控制器
 * </p>
 *
 * @author Guo.wl
 * @since 2019-11-26
 */
@Controller
@RequestMapping("/sys/log")
public class SysLogController {
    @Autowired
    private SysLogService logService;

    /**
     * @return index
     */
    @AppLog(title = "通用日志查看")
    @PreAuthorize("hasAuthority('sys:log')")
    @GetMapping("/list")
    public String index() {
        return "sys/log";
    }

    @PreAuthorize("hasAuthority('sys:log')")
    @ResponseBody
    @GetMapping
    public Result logList() {
        // 得到所有的查询参数，注意也包括分页信息和排序信息
        QueryParam qp = RequestUtil.getQueryParam();
        // 组装分页信息
        PageParam pageParam = qp.getPageParamByNumAndSize("page", "rows");
        // 组装排序信息
        String orderBy = qp.getOrderBy("sort", "order", true);

        IPage<SysLog> page = logService.findPage(qp, pageParam, orderBy);
        GridTable<SysLog> table = new GridTable<>(page.getTotal(), page.getRecords());

        return Result.success(table);
    }


}